package me.aartikov.screenresultsample.ui;

import me.aartikov.alligator.NavigationContext;
import me.aartikov.alligator.NavigationContextBinder;
import me.aartikov.alligator.Navigator;
import me.aartikov.screenresultsample.ResourceTable;
import me.aartikov.screenresultsample.SampleApplication;
import me.aartikov.screenresultsample.screens.MessageInputScreen;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;


public class MessageInputAbilitySlice extends AbilitySlice {
    private Navigator mNavigator = SampleApplication.getNavigator();
    private NavigationContextBinder mNavigationContextBinder = SampleApplication.getNavigationContextBinder();

    TextField mMessageEditText;
    Button mOkButton;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_message_input);
        initView();
    }

    private void initView() {
        mOkButton = (Button) findComponentById(ResourceTable.Id_ok_button);
        mMessageEditText = (TextField) findComponentById(ResourceTable.Id_message_edit_text);

        mOkButton.setClickedListener(v -> {
            String message = mMessageEditText.getText().toString();
            mNavigator.goBackWithResult(new MessageInputScreen.Result(message));    // Easy-peasy!
        });
    }


    @Override
    public void onActive() {
        super.onActive();
        NavigationContext navigationContext = new NavigationContext.Builder(getAbility(), SampleApplication.getNavigationFactory())
                .build();
        mNavigationContextBinder.bind(navigationContext);
    }

    @Override
    protected void onInactive() {
        mNavigationContextBinder.unbind(getAbility());
        super.onInactive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    @Override
    protected void onBackPressed() {
        mNavigator.goBack();
    }
}
